import request from '../request'
import type { ApiResponse, OrderListResponse, OrderCountResponse } from '../types'
import type { CartItem } from '@/types/cart'

// 获取购物车列表
export const getCartList = () => {
  return request.get<ApiResponse<CartItem[]>>('/app/order/cart/listCart')
}

// 添加商品到购物车
export const addToCart = (data: { goodsId: string; count: number }) => {
  return request.post<ApiResponse>('/app/order/cart/addCart', data)
}

// 删除购物车商品
export const deleteCart = (data: { id: number }) => {
  return request.post<ApiResponse>('/app/order/cart/remove', {
    ids: [data.id]
  })
}

// 修改购物车商品数量
export const updateCart = (data: { id: number; count: number }) => {
  return request.post<ApiResponse>('/app/order/cart/updateCount', data)
}

//清空购物车
export const clearCart = () => {
  return request.post<ApiResponse>('/app/order/cart/clear')
}

// 从购物车创建订单
export const createFromCart = (data: { cartIds: number[], mjBillNo?: string }) => {
  return request.post<ApiResponse>('/app/order/info/createFromCart', data)
}

// 获取订单列表
export const getOrderList = (data: { page: number; size: number }) => {
  return request.post<OrderListResponse>('/app/order/info/page', {
    params: data
  })
}

// 统计订单数量
export const getOrderCount = () => {
  return request.get<OrderCountResponse>('/app/order/info/userCount')
}
